package com.pet.admin.controller;

import com.pet.admin.mapper.ActivityMapper;
import com.pet.admin.mapper.EnrollMapper;
import com.pet.admin.model.Activity;
import com.pet.admin.model.ActivityExample;
import com.pet.admin.model.Enroll;
import com.pet.admin.model.EnrollExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private EnrollMapper enrollMapper;
   @RequestMapping(value="/index",method = RequestMethod.GET)
   public String index(Model model) {

      return "index";  
   }

    @RequestMapping(value="/api/home",method = RequestMethod.GET)
    public String home(Model model) {

        return "home";
    }

    @RequestMapping(value="/logout",method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("adminEmail");
        return "index";
    }

    @RequestMapping(value="/api/activity",method = RequestMethod.GET)
    public String activity(HttpServletRequest request,Model model) {
        List<Activity> activities = activityMapper.selectByExample(new ActivityExample());
        model.addAttribute("activities",activities);
        return "activity";
    }
    @RequestMapping(value="/api/actEnroll",method = RequestMethod.GET)
    public String actEnroll(HttpServletRequest request,Model model) {
        String actId = request.getParameter("id");
        EnrollExample enrollExample = new EnrollExample();
        enrollExample.createCriteria().andActivityidEqualTo(Integer.parseInt(actId));
        List<Enroll> enrolls = enrollMapper.selectByExample(enrollExample);
        model.addAttribute("enrolls",enrolls);
        return "enroll";
    }

    @RequestMapping(value="/editact",method = RequestMethod.POST)
    public void editact(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam("pic") MultipartFile pic) throws ParseException, IOException {
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        String desc = request.getParameter("desc");
        String time = request.getParameter("time");
        String location = request.getParameter("location");

        ActivityExample activityExample = new ActivityExample();
        Activity activity = new Activity();

        if(!pic.isEmpty()) {
            String picname = pic.getOriginalFilename();
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(new File("D://" + picname)));
            out.write(pic.getBytes());
            out.flush();
            out.close();
            activity.setPic(picname);
        }

        activity.setId(Integer.parseInt(id));
        activity.setActtime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time));
        activity.setLocation(location);
        activity.setDesctxt(desc);
        activity.setTitle(title);
        activityExample.createCriteria().andIdEqualTo(Integer.parseInt(id));
        activityMapper.updateByExampleSelective(activity,activityExample);
        response.sendRedirect(request.getContextPath()+"/api/activity");
    }

    @RequestMapping(value="/addact",method = RequestMethod.POST)
    public void addact(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam("pic") MultipartFile pic) throws ParseException, IOException {
        String title = request.getParameter("title");
        String desc = request.getParameter("desc");
        String time = request.getParameter("time");
        String location = request.getParameter("location");
        String picname = pic.getOriginalFilename();
        BufferedOutputStream out = new BufferedOutputStream(
                new FileOutputStream(new File("D://" + picname)));
        out.write(pic.getBytes());
        out.flush();
        out.close();


        Activity activity = new Activity();
        activity.setTitle(title);
        activity.setDesctxt(desc);
        activity.setLocation(location);
        activity.setPic(picname);
        activity.setActtime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time));
        activityMapper.insertSelective(activity);

        response.sendRedirect(request.getContextPath()+"/api/activity");
    }

}